package com.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.DAO.UserLoginDAO;
import com.boot.DAO.UserPageDAO;
import com.boot.pojo.CustomUserDetail;
import com.boot.pojo.User;
import com.boot.result.Result;
import com.boot.service.UserService;
import com.boot.mapper.UserMapper;
import com.boot.utils.JwtUtil;
import com.boot.utils.MD5Util;
import com.boot.utils.PasswordUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author liyan
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2025-05-01 16:03:22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private UserMapper userMapper;

    private PasswordEncoder getPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    public Result loginUser(UserLoginDAO userLoginDAO) {
        User user = getUserByName(userLoginDAO.getUsername());
        if (user == null) {
            return Result.build(null,10002, "username not existed");
        }
        boolean isMatch = PasswordUtil.matches(userLoginDAO.getPassword(), user.getPassword());
        if (isMatch) {
            String token = jwtUtil.createToken(user.getId());
            return Result.success(Map.of("token", token));
        }
        return Result.build(null,10003, "password error");
    }

    @Override
    public User getUserById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public User getUserByName(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Result registerUser(UserLoginDAO userLoginDAO) {
        User user = new User();
        user.setUsername(userLoginDAO.getUsername());
        user.setPassword(PasswordUtil.encode(userLoginDAO.getPassword()));
        int insertRow = userMapper.insert(user);
        if (insertRow > 0) {
            return Result.success();
        }
        return Result.build(null, 10001, "create user failed");
    }

    @Override
    public CustomUserDetail selectUserDetailByUsername(String username) {
        return userMapper.selectUserDetailByUsername(username);
    }

    @Override
    public CustomUserDetail selectUserDetailById(Long id) {
        return userMapper.selectUserDetailById(id);
    }

    @Override
    public Result pageUsers(UserPageDAO userPageDAO) {
        IPage<User> page = new Page<>(userPageDAO.getPage(), userPageDAO.getPageSize());
        userMapper.selectMyPage(page, userPageDAO);
        return Result.success(Map.of("total", page.getTotal(),
        "lists",page.getRecords()));
    }
}




